package com.cy.pj.sys.controller;


import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.util.PageUtil;
import com.cy.pj.sys.pojo.JsonResult;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/log/")
public class SysLogController {
    @Autowired
    private SysLogService sysLogService;

    @GetMapping("{id}")
    public JsonResult doFindById(@PathVariable("id") Long id){
        return new JsonResult(sysLogService.findById(id));
    }

    @PostMapping
    public JsonResult doSaveLogs(@RequestBody SysLog sysLog){
        sysLogService.saveLog(sysLog);
        return new JsonResult("save ok");//json序列化(将对象转换为json格式字符串
    }

    @GetMapping
    public JsonResult doFindLogs(SysLog sysLog){
        Object o =  PageUtil.startPage().doSelectPageInfo(()->sysLogService.findLogs(sysLog));
        return new JsonResult(o);
    }

    @DeleteMapping("{ids}")
    public JsonResult doSelectLogs(@PathVariable Long... ids){
        int rows = sysLogService.deleteById(ids);
        if (rows==0){
            //throw new RuntimeException("记录可能已经不存在了");
            throw new ServiceException("记录可能已经不存在了");
        }
        return new JsonResult("delete ok");
    }
}
